package logic

import (
	"class/classrpc/classrpc"
	"class/classrpc/internal/svc"
	"class/models"
	"class/pkg"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 评论添加
func (l *CommentAddLogic) CommentAdd(in *classrpc.CommentAddRequest) (*classrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line

	var user models.User
	models.DB.Debug().Where("id = ?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该用户不存在,评论失败")
	}

	var course models.Course
	models.DB.Debug().Where("id = ?", in.CourseId).Find(&course)
	if course.ID == 0 {
		return nil, fmt.Errorf("该课程不存在")
	}

	if pkg.GetWord(in.Content) == true {
		return nil, fmt.Errorf("评论包含敏感词汇,评论失败")
	}

	comments := models.Comment{
		Content:  in.Content,
		CourseId: int(in.CourseId),
		UserId:   int(in.UserId),
	}

	models.DB.Debug().Create(&comments)

	return &classrpc.CommentAddResponse{
		CommentAdd: int64(comments.ID),
	}, nil
}
